from selenium import webdriver
from selenium.webdriver.common.by import By
from options import options
from time import sleep


class ASMS():
    def __init__(self, driver):
        self.driver = driver

    def login(self, username, password, id='1', node='shtd', ):
        driver.find_element(By.ID, 'taskId').clear()
        driver.find_element(By.ID, 'taskId').send_keys(id)
        driver.find_element(By.ID, 'loginName').clear()
        driver.find_element(By.ID, 'loginName').send_keys(username)
        driver.find_element(By.ID, 'password').clear()
        driver.find_element(By.ID, 'password').send_keys(password)
        driver.find_element(By.ID, 'vericode').clear()
        driver.find_element(By.ID, 'vericode').send_keys(node)
        driver.find_element(By.XPATH, '//*[@id="fmedit"]/div[2]/div[6]/input').click()
        sleep(3)

    def logout(self, username, password):
        self.login(username, password)
        driver.find_element(By.LINK_TEXT, '退出').click()
        sleep(3)

    def setphone(self, phone, username, password):
        self.login(username, password)
        sleep(1)
        driver.find_element(By.ID, 'phone').clear()
        driver.find_element(By.ID, 'phone').send_keys(phone)
        driver.find_element(By.LINK_TEXT, '保存').click()
        driver.switch_to.alert.accept()
        sleep(2)


if __name__ == '__main__':
    url = r'http://172.16.22.105:8080/bsams/front/login.do'
    driver = webdriver.Chrome(options=options)
    driver.get(url)
    driver.maximize_window()

    asms = ASMS(driver)

    # 测试登录
    username = 'test16'
    password = '123456'
    # asms.login(username, password)
    # # 测试退出
    # asms.logout(username, password)

    # 测试修改手机号
    phone = '12345678901'
    asms.setphone(phone, username, password)
